package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrdersServiceimpl extends ServiceImpl<OrdersMapper,Orders> implements OrdersService {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    @Transactional
    public void submit(Orders orders) {
        //A. 获得当前用户id, 查询当前用户的购物车数据
        Long userid = BaseContext.getid();

        //查询当前用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        List<ShoppingCart> shoppingCart = shoppingCartService.list(queryWrapper);
        if (shoppingCart==null || shoppingCart.size()==0){
            throw new ClassCastException("购物车不能为空，请重试");
        }
        //查询用户数据
        User byId = userService.getById(userid);

        //查询地址数据
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if (addressBook==null){
            throw new ClassCastException("地址为空，请重试");
        }
        long id = IdWorker.getId();//订单号
        //初始化金额
        AtomicInteger amount = new AtomicInteger(0);
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCart cart : shoppingCart) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(id);//使用刚刚生产的订单号设置ID
            orderDetail.setNumber(cart.getNumber());//调用购物车的菜品或者套餐个数设置新的数据
            orderDetail.setDishFlavor(cart.getDishFlavor());//调用购物车的菜品或者套餐设置新的口味
            orderDetail.setDishId(cart.getDishId());//调用购物车的菜品设置新的菜品ID
            orderDetail.setSetmealId(cart.getSetmealId());//调用购物车的套餐设置新的套餐ID
            orderDetail.setName(cart.getName());//调用购物车的数据设置用户名
            orderDetail.setImage(cart.getImage());//调用购物车的数据设置新的图片信息
            orderDetail.setAmount(cart.getAmount());
            amount.addAndGet(cart.getAmount().multiply(new BigDecimal(cart.getNumber())).intValue());
            orderDetails.add(orderDetail);
        }
        orders.setId(id);
        orders.setOrderTime(LocalDateTime.now());//设置下单时间
        orders.setCheckoutTime(LocalDateTime.now());//设置结账时间
        orders.setStatus(2);//设置订单状态
        orders.setAmount(new BigDecimal(amount.get()));//设置总金额
        orders.setUserId(userid);//设置用户ID
        orders.setNumber(String.valueOf(id));//设置数量
        orders.setUserName(byId.getName());//设置用户名字
        orders.setConsignee(addressBook.getConsignee());//使用地址簿的创建人设置收货人
        orders.setPhone(addressBook.getPhone());//使用地址簿的手机号码设置手机号
        //使用地址簿中的省级名称设置地址
        orders.setAddress((addressBook.getProvinceName()==null ?"":addressBook.getProvinceName())
                +(addressBook.getCityName()==null ?"":addressBook.getCityName())
                +(addressBook.getDistrictName()==null?"":addressBook.getDistrictName())
                +(addressBook.getDetail()==null?"":addressBook.getDetail()));
        // D. 组装订单明细数据, 批量保存订单明细
        this.save(orders);
        // E. 组装订单数据, 批量保存订单数据
        orderDetailService.saveBatch(orderDetails);
        // F. 删除当前用户的购物车列表数据
        shoppingCartService.remove(queryWrapper);
        // B. 根据当前登录用户id, 查询用户数据
        // C. 根据地址ID, 查询地址数据





    }
}
